package com.itheima.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author slstart
 * @create 2021-05-23 17:43
 */
public class BaseServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //1.获取请求中的method参数值
            String methodName = request.getParameter("method");
            //2.获取调用者类的字节码对象
            Class<? extends BaseServlet> clazz = this.getClass();
            //3.获取指定方法名和参数的方法对象
            Method method = clazz.getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
            //4.调用方法
            method.invoke(this,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}